//InsureClearingModel
//2020-08-25 09:22:09

package models

import (
	"git.900sui.cn/kc/base/common/functions"
	"git.900sui.cn/kc/base/common/models/base"
	"git.900sui.cn/kc/kcgin/orm"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
)

//表结构体
type InsureClearingModel struct {
	Model *base.Model
	Field InsureClearingModelField
}

//表字段
type InsureClearingModelField struct {
	T_table       string `default:"insure_clearing"`
	F_id          string `default:"id"`
	F_insure_id   string `default:"insure_id"`
	F_bill_date   string `default:"bill_date"`
	F_amount      string `default:"amount"`
	F_status      string `default:"status"`
	F_agent_type  string `default:"agent_type"`
	F_agent_id    string `default:"agent_id"`
	F_create_time string `default:"create_time"`
}

//初始化
func (i *InsureClearingModel) Init(ormer ...orm.Ormer) *InsureClearingModel {
	functions.ReflectModel(&i.Field)
	i.Model = base.NewModel(i.Field.T_table, ormer...)
	return i
}

//新增数据
func (i *InsureClearingModel) Insert(data map[string]interface{}) int {
	result, _ := i.Model.Data(data).Insert()
	return result
}

// 根据bus_id和billDate查询
func (i *InsureClearingModel) FindByBusIdAndBillDate(insureId, billDate int) map[string]interface{} {
	result := i.Model.Where(map[string]interface{}{
		i.Field.F_insure_id: insureId,
		i.Field.F_bill_date: billDate,
	}).Find()
	return result
}

// 根据bus_id和billDate更新
func (b *InsureClearingModel) UpdateByInsureIdAndBillDate(insureId, billDate int, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_insure_id: insureId,
		b.Field.F_bill_date: billDate,
	}).Data(data).Update()
}

// 根据id和status更新
func (b *InsureClearingModel) UpdateByIdAndStatus(id, status interface{}, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_id:     id,
		b.Field.F_status: status,
	}).Data(data).Update()
}

// 根据insureId,agentId，agentType，status更新数据
func (b *InsureClearingModel) UpdateAgent(insureId, agentId, agentType, status interface{}, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_insure_id:  insureId,
		b.Field.F_agent_id:   agentId,
		b.Field.F_agent_type: agentType,
		b.Field.F_status:     status,
	}).Data(data).Update()
}

// 根据状态获取分页数据
func (b *InsureClearingModel) Lists(billDate string, start, limit int) []map[string]interface{} {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_status:    pay.CLEAR_STATUS_none,
		b.Field.F_bill_date: []interface{}{"lt", billDate},
	}).Limit(start, limit).OrderBy(b.Field.F_insure_id).Select()
}
